/* nLayers (c) 
 * Created on Jan 29, 2004
 */
package dnl.util.ui.table.sort;

import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

/**
 * 
 * 
 * @author <a href="mailto:daniel@nlayers.com">Daniel Or</a>
 */
public final class SortDirection  { 

	public static final SortDirection Descending =  new SortDirection("Descending");
	public static final SortDirection Ascending =  new SortDirection("Ascending");

	/**
	 * Return a description of the sort order, suitable for presentation 
	 * to the user.
	 */
	@Override
	public String toString() { 
		return fName;  
	} 

	/** 
	 * Convert <code>aText</code> into its corresponding 
	 * <code>SortOrder</code> object, if possible.
	 *
	 * @param aText possibly-null text which may map to a <code>SortOrder</code>.
	 * @return null if <code>aText</code> is null, else try to
	 * match to a known <code>SortOrder</code>.
	 * @throws IllegalArgumentException if non-null <code>aText</code> cannot be matched to 
	 * a known enumeration element.
	 */
	public static SortDirection valueOf(String aText) { 
		if (aText == null) return null; 
		Iterator<?> valuesIter = VALUES.iterator(); 
		SortDirection item = null; 
		while( valuesIter.hasNext() ) { 
			item = (SortDirection)valuesIter.next(); 
			if ( aText.equals( item.toString() ) ) { 
				return item; 
			} 
		} 
		throw new IllegalArgumentException("Cannot parse into SortOrder object:" + aText); 
	} 

	/**
	 * Return the opposite <code>SortOrder</code> from <code>this</code> one.
	 */
	public SortDirection toggle(){
		return (this == Ascending ? Descending : Ascending);
	}
	
	/// PRIVATE /// 
	private final  String fName;
	
	private SortDirection (String aName) { 
		fName = aName;
	}

	private static final SortDirection[] fValues = { Descending, Ascending };
	
	/**
	 * Allows caller to iterate over all elements of the enumeration.
	 */
	public static final List<?> VALUES = Collections.unmodifiableList(Arrays.asList(fValues));
} 
